#define _CRT_SECURE_NO_WARNINGS 1
#include "ipmaclist.h"

// 创建ip和mac链表存储头节点
ipMac* createIpMacHead(void)
{
	ipMac* ipMacHead = (ipMac*)malloc(sizeof(ipMac));
	// 判断创建节点是否为NULL
	if (ipMacHead->next == NULL && ipMacHead == NULL) {
		return ipMacHead;
	}

	memset(ipMacHead, 0, sizeof(ipMac));

	return ipMacHead;
}

// 插入读取的ip和mac数据
int insertIpMacData(ipMac* ipMacHead, uc8* ipData, uc8* macData, ipMacSize* dataSize)
{
	if (ipMacHead == NULL) {
		printf("没有ip和mac的头结点");
		DEBUGERROR;
		return 0;
	}

	if (dataSize == NULL) {
		printf("没有ip和mac的数据大小");
		DEBUGERROR;
		return 0;
	}

	ipMac* p = ipMacHead;
	ipMac* q = (ipMac*)malloc(sizeof(ipMac));
	q->next = NULL;

	if (q != NULL) {
    	memset(q, 0, sizeof(ipMac));
	}

	// 存储数据进行空间开辟
	q->ipInf = (uc8*)malloc(sizeof(uc8) * (dataSize->ipSize));
	q->macInf = (uc8*)malloc(sizeof(uc8) * (dataSize->macSize));

	if (q->ipInf == NULL || q->macInf == NULL) {
		free(q->ipInf);
		free(q->macInf);
		free(q);
		printf("内存分配失败");
		DEBUGERROR;
		return -1;
	}

	// ip和mac数据存储
	if(ipData == NULL){
		strcpy(q->ipInf, "   ");
	}else{
		strcpy(q->ipInf, ipData);
	}
	q->ipInf[dataSize->ipSize - 1] = '\0'; // 添加字符串结束符

	if(macData == NULL){
		strcpy(q->macInf, "   ");
	}else{
		strcpy(q->macInf, macData);
	}
	q->macInf[dataSize->ipSize - 1] = '\0'; // 添加字符串结束符


	if (q->ipInf == NULL && q->macInf == NULL) {
		printf("数据复制失败");
		DEBUGERROR;
		return -1;
	}

	// 插入数据,头插法
	q->next = p->next;
	p->next = q;

	// 成功,返回1
	return 1;
}

// 删除链表中的ip和mac数据
int deleteIpMacData(ipMac* ipMacHead, uc8* ipData, uc8* macData)
{
	if (ipMacHead == NULL) {
		printf("没有ip和mac的头结点");
		DEBUGERROR;
		return 0;
	}

	if (ipData == NULL && macData == NULL) {
		printf("没有需要删除ip和mac的信息");
		DEBUGERROR;
		return 0;
	}

	ipMac* p = ipMacHead;
	ipMac* q;
	if (ipData != NULL) {
		while (p->next != NULL) {
			q = p->next;
			if (strcmp(q->ipInf, ipData) == 0) {
				p->next = q->next;
				free(q);
				printf("ip和mac删除完成\n");
				return 1;
			}
			p = p->next;
		}
	} else if (ipData == NULL && macData != NULL) {
		while (p->next != NULL) {
			q = p->next;
			if (strcmp(q->macInf, macData) == 0) {
				p->next = q->next;
				free(q);
				printf("ip和mac删除完成\n");
				return 1;
			}
			p = p->next;
		}
	}

	printf("没有找到需要删除的ip和mac数据\n");
	return 0;
}

// 销毁ip和mac存储的链表
int destoryIpMacList(ipMac** ipMacHead)
{
	if (*ipMacHead == NULL) {
		printf("没有头结点");
		DEBUGERROR;
		return 0;
	}

	ipMac* p = (*ipMacHead)->next;
	ipMac* q;

	while (p != NULL) {
		q = p->next;
		free(p);
		p = q;
	}
	
	(*ipMacHead) = NULL;
	return 1;
}

// 计算链表的长度
int lengthIpMacList(ipMac* ipMacHead)
{
	if (ipMacHead == NULL) {
		printf("没有头结点");
		DEBUGERROR;
		return 0; // 返回0表示为空链表
	}

	ipMac* p = ipMacHead->next;
	int len = 0;
	while (p != NULL) {
		++len;
		p = p->next;
	}

	return len;
}

// 显示链表数据
int displayIpMacList(ipMac* ipMacHead)
{
	if (ipMacHead == NULL) {
		printf("没有头结点");
		DEBUGERROR;
		return 0;
	}

	ipMac* p = ipMacHead->next;

	while(p != NULL){
		printf("#===========================#\n");
		printf("ip: %s\nmac: %s\n", p->ipInf, p->macInf);
		p = p->next;
	}

	return 1;
}

// 遍历链表中的IP和MAC值
// 存在返回1,反之0
// 查询时, ipData、macData必须有一个值传入
int queryIpMacExist(ipMac* ipMacHead, uc8* ipData, uc8* macData)
{
	if(ipMacHead == NULL){
		//printf("不存在值\n");
		return 0;
	}

	ipMac* p = ipMacHead->next;
	// 遍历查询值
	while(p != NULL){
		if(ipData != NULL){
			if(strcmp(p->ipInf, ipData) == 0){
				//printf("ip地址存在\n");
				return 1;
			}
		}
		
		if(macData != NULL){
			if(strcmp(p->macInf, macData) == 0){
				//printf("mac地址存在\n");
				return 1;
			}
		}
		
		p = p->next;
	}
	//printf("不存在值\n");

	return 0;
}


